package v1

import (
	"github.com/gin-gonic/gin"
	"manager/bll"
	"manager/model"
	"manager/server/web/middleware"
	"manager/utils"
)

var ShareEbike = &shareEbike{}

func init() {
	// 注册路由
	RegisterRouter(ShareEbike)
}

type shareEbike struct{}

// Init 初始化路由
func (a *shareEbike) Init(r *gin.RouterGroup) {
	/*	// web 路由
		g := r.Group("/share_ebike", middleware.Auth())
		{
			g.POST("/create", a.create)
			g.POST("/update", a.update)
			g.POST("/list", a.list)
			g.POST("/delete", a.delete)
		}*/
	// app 路由
	appG := r.Group("app/share_device", middleware.Auth())
	{
		appG.POST("/create", a.create) //创建分享
		appG.POST("/update", a.update)
		appG.POST("/accept", a.accept) // 接受分享
		appG.POST("/list", a.list)     // 分享列表
		appG.POST("/user_share", a.shareList)
		appG.POST("/delete", a.delete) // 取消分享
	}
}

// create 创建
func (a *shareEbike) create(c *gin.Context) {
	defer utils.ExceptionCatch()
	var (
		in  = &model.ShareEbikeCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.ShareEbike.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update 更新
func (a *shareEbike) update(c *gin.Context) {
	var (
		in  = &model.ShareEbikeUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.ShareEbike.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list 列表查询
func (a *shareEbike) list(c *gin.Context) {
	var (
		in  = &model.ShareEbikeListRequest{}
		out = &model.ShareEbikeListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.ShareEbike.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// delete 删除
func (a *shareEbike) delete(c *gin.Context) {
	var (
		in  = &model.ShareEbikeDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.ShareEbike.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *shareEbike) accept(c *gin.Context) {
	var (
		in  = &model.ShareEbikeBaseRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.ShareEbike.Accept(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

func (a *shareEbike) shareList(c *gin.Context) {
	var (
		out *model.ShareEbikeListAllResponse
		in  = &model.ShareAllRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.ShareEbike.ShareList(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}
